<template>
  <su-page class="su-login">
    <su-body>
      <view class="login-body">
        <!-- 登录form -->
        <passwordLogin v-if="activeType==='0'" :loading="loading" @login="doLogin"></passwordLogin>
        <!-- 注册按钮 -->
        <!-- <view class="login-signUp-btn mt20">
          <u-button size="large" text="注册" @click="goToSignUp"></u-button>
        </view> -->
      </view>
    </su-body>
  </su-page>
</template>

<script>
  /**
   * @description 登录
   * 设计图 1-1 1-2 1-3
   * @date 2022-03-12
   * @version v1.0.1
   * @author gf
   */
   import md5 from 'js-md5'
   import {runSQL} from '@/common/utils/wxutils.js'
 
  import {
    login
  } from '@/common/api.js'
  import passwordLogin from './components/passwordLogin.vue'

  export default {
    components: {
      passwordLogin
    },
    data() {
      return {
        loading: false,
        activeType: '0',
      }
    },
    onLoad(params) {
      if (this.token) {
        this.goHome()
      }
    },
    onUnload() {
      this.$store.commit('commonSet', {
        key: 'tokenInvalidFlag',
        value: false
      })
    },
    onBackPress(options) {
      return true
    },
    methods: {
      // 系统登录
      doLogin(params) {
				params.password = (params.password.length == 32 ? params.password : md5(params.password))
				runSQL("select * from tbhuman where (humanname='"+params.username+"' or telephone='"+params.username+"' or  humancode='"+params.username+"') and password='"+params.password+"';").then(res=>{
				//console.log('登录参数1', (res[0].length>0))
				//console.log('登录参数2',) ("select * from tbhuman where (humanname='"+params.username+"' or telephone='"+params.username+"' or or humancode='"+params.username+"') and password='"+md5(params.password)+"';")) 
					if (res[0].length>0) {
						uni.setStorageSync('userInfo', JSON.stringify(res[0][0]));
						runSQL("select * from ssnydw.tbrole a,ssnydw.tbrolehuman  b where a.roleid=b.roleid and humanid="+res[0][0].HUMANID+" and a.roleid=14353;").then(ress=>{
							if(ress[0].length>0){
								//项目经理
								this.loading = true
								setTimeout(()=>{
									this.loading = false
									//if (res.data.token) {
									//登录成功，存储token
									this.$store.commit('setToken', res[0][0].HUMANID)
									this.$store.commit('setUserInfo', res[0]);
									uni.setStorageSync('auth', 1);//1 为项目经理
									this.goHome(1)
									//}
								},200)
							}else{
								runSQL("select * from ssnydw.tbunit  where  unitid="+res[0][0].UNITID+" and (unitid=1991 or parentid=1991);").then(rea=>{
									if(rea[0].length>0){
										// uni.showToast({
										// 	title: '登录失败，请用项目经理账号登录！',
										// 	icon:'none',
										// 	duration: 2000
										// });
										setTimeout(()=>{
											this.loading = false
											//if (res.data.token) {
											//登录成功，存储token
											this.$store.commit('setToken', res[0][0].HUMANID)
											this.$store.commit('setUserInfo', res[0]);
											uni.setStorageSync('auth', 3);//3 企业账号
											this.goHome(3)
											//}
										},200)
									}else{
										setTimeout(()=>{
											this.loading = false
											//if (res.data.token) {
											//登录成功，存储token
											this.$store.commit('setToken', res[0][0].HUMANID)
											this.$store.commit('setUserInfo', res[0]);
											uni.setStorageSync('auth', 2);//2 为主管部门
											this.goHome(2)
											//}
										},200)
									}
								})
							}
						})
					}else{
						uni.showToast({
						title: '登录失败，账号或密码错误！',
						icon:'none',
						duration: 2000
						});
					}
				});  
      },
      // 跳转首页
      goHome(a) { 
        this.$u.route({ 
          type: 'switchTab', 
          url: '/pages/main/home/home',
		  type:'reLaunch',
		  params:{
		    id:a
		  }
        })
      },
      // 跳转注册页面
      goToSignUp() {
        this.$u.route({
          url: '/pages/package2/signUp/signUp'
        })
      }
    }
  }
</script>

<style lang="scss">
  @import "./components/login.scss";
</style>
